from email.mime import audio
from pyexpat import model
from unittest import result
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
import time
from pydub import AudioSegment
import requests
import json
import datetime
import time


inference_pipelines = None


def AudioPipelines():
    global inference_pipelines
    inference_pipelines = pipeline(
        task=Tasks.auto_speech_recognition,
        model='damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch')


def AudioRecognize():
    global inference_pipelines
    if inference_pipelines is not None:
        #audio = AudioSegment.from_mp3('/home/mhc/out.mp3')
        #audio.export('/home/mhc/out.wav', format='wav')
        result = inference_pipelines('/home/mhc/out.wav')
        print(result)
        return result
    else:
        return ''

# 获取当前日期
def get_current_date():
    current_date = datetime.date.today()
    print("当前日期:", current_date)
    return current_date


# 获取当前星期（0 代表星期一，6 代表星期日）
def get_current_weekday():
    current_date = datetime.date.today()
    current_weekday = current_date.weekday()
    if current_weekday == 0:
        print("当前星期:星期一")
        return "星期一"
    elif current_weekday == 1:
        print("当前星期:星期二")
        return "星期二"
    elif current_weekday == 2:
        print("当前星期:星期三")
        return "星期三"
    elif current_weekday == 3:
        print("当前星期:星期四")
        return "星期四"
    elif current_weekday == 4:
        print("当前星期:星期五")
        return "星期五"
    elif current_weekday == 5:
        print("当前星期:星期六")
        return "星期六"
    elif current_weekday == 6:
        print("当前星期:星期日")
        return "星期日"


# 获取当前时间
def get_current_time():
    current_time = time.strftime("%H:%M:%S", time.localtime())
    print("当前时间:", current_time)
    return current_time


# 使用高德天气API获取当前天气(暂时以武汉为例，其余城市查看高德天气API文档城市编码即可)
def get_current_weather():
    key = "d0caf1b55c83d6b3bf3e98c3750e9f7a"
    # 武汉
    city = '420100'
    url = "https://restapi.amap.com/v3/weather/weatherInfo?city=" + city + "&key=" + key
    response = requests.get(url)
    response.encoding = 'utf-8'
    data = json.loads(response.text)
    # 检查是否获取成功
    if data['status'] == '1':
        # 获取温度
        temperature = data['lives'][0]['temperature_float']
        print("当前温度:", temperature)
        # 获取湿度
        humidity = data['lives'][0]['humidity_float']
        print("当前湿度:", humidity)
        # 获取天气信息
        weather = data['lives'][0]['weather']
        print("当前天气:", weather)
        info = str(temperature) + "-" + str(humidity) + "-" + weather
        print("天气信息:", info)
        return info
    else:
        print("获取天气信息失败")
        return "获取天气信息失败"


if __name__ == '__main__':
    AudioPipelines()
    AudioRecognize()



